﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyNhaSach.Customer
{
    public partial class FrmCustomerDetail : DevComponents.DotNetBar.Office2007Form
    {
        CustomerBUS customerBus;

        public ActionName Action { get; set; }
        public CustomerDTO CustomerDto { get; set; }

        public FrmCustomerDetail()
        {
            InitializeComponent();
            customerBus = new CustomerBUS();
            btnUpdate.Location = btnAdd.Location;
        }

        private void FrmCustomerDetail_Load(object sender, EventArgs e)
        {
            dtpCustomerBirthday.Format = DevComponents.Editors.eDateTimePickerFormat.Custom;
            dtpCustomerBeginDay.Format = DevComponents.Editors.eDateTimePickerFormat.Custom;
            dtpCustomerBirthday.CustomFormat = "dd/MM/yyyy";
            dtpCustomerBeginDay.CustomFormat = "dd/MM/yyyy";
            dbiDebt.Enabled = false;

            if (Action == ActionName.Insert)
            {
                DocSequenceBUS docsequenceBus = new DocSequenceBUS();
                txtCustomerCode.Text = docsequenceBus.GetNextDocSequenceNumber(DocSequence.Customer.ToString());
       
                rdbMale.Checked = true;
                dtpCustomerBeginDay.Value = DateTime.Now;
                txtCustomerName.Focus();
                CustomerDto = new CustomerDTO();
            }
            else
            {
                txtCustomerCode.Text = CustomerDto.CustomerCode;
                txtCustomerName.Text = CustomerDto.CustomerName;
                dtpCustomerBirthday.Value = (DateTime)CustomerDto.CustomerBirthday;
                txtCustomerAddress.Text = CustomerDto.CustomerAddress;
                txtCustomerPhone.Text = CustomerDto.CustomerPhone;
                txtCustomerEmail.Text = CustomerDto.CustomerEmail;
                if(CustomerDto.CustomerSex)
                {
                    rdbMale.Checked = true;
                    rdbFemale.Checked = false;
                }
                else{
                    rdbMale.Checked = false;
                    rdbFemale.Checked = true;
                }
                txtCustomerCMND.Text = CustomerDto.CustomerCMND;
                dtpCustomerBeginDay.Value =(DateTime) CustomerDto.CustomerBeginDay;
                dbiDebt.Value = CustomerDto.CustomerDebtMoney;
                txtCustomerNote.Text = CustomerDto.Note;
            }

            ProcessButton();
        }

        private void ProcessButton()
        {
            if (Action == ActionName.Insert)
            {
                btnUpdate.Visible = false;
                btnAdd.Visible = true;
                dtpCustomerBeginDay.Enabled = true;
            }
            else if (Action == ActionName.Update)
            {
                dtpCustomerBeginDay.Enabled = false;
                btnUpdate.Visible = true;
                btnAdd.Visible = false;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
            {
                return;
            }

            CustomerDto.CustomerCode = txtCustomerCode.Text;
            CustomerDto.CustomerName = txtCustomerName.Text;
            CustomerDto.CustomerBirthday = dtpCustomerBirthday.Value;
            CustomerDto.CustomerAddress = txtCustomerAddress.Text;
            CustomerDto.CustomerPhone = txtCustomerPhone.Text;
            CustomerDto.CustomerEmail = txtCustomerEmail.Text;
            CustomerDto.CustomerSex = rdbMale.Checked;
            CustomerDto.CustomerCMND = txtCustomerCMND.Text;
            CustomerDto.CustomerBeginDay = dtpCustomerBeginDay.Value;
            CustomerDto.CustomerDebtMoney = 0;
            CustomerDto.Note = txtCustomerNote.Text;
            CustomerDto.Status = true;

            CustomerDto.CustomerID = customerBus.InsertCustomer(CustomerDto);
            if (CustomerDto.CustomerID > 0)
            {
                Global.SetMessage(lblMessage, "Thêm thành công!", true);
                Action = ActionName.Update;
                ProcessButton();

                Customer.FrmCustomerList frmList = (Customer.FrmCustomerList)Application.OpenForms["FrmCustomerList"];
                if (frmList != null)
                {
                    frmList.CustomerChangeAfterInsert(CustomerDto);
                }
                //OnCategoryChanged(Category);
            }
            else
            {
                Global.SetMessage(lblMessage, "Thêm không thành công!", false);
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            ResetForm();
            Action = ActionName.Insert;
            ProcessButton();
            CustomerDto = new CustomerDTO();
        }

        public void ResetForm()
        {
            DocSequenceBUS docSeqBUS = new DocSequenceBUS();
            txtCustomerName.Text = string.Empty;
            txtCustomerName.Focus();
            txtCustomerCode.Text = docSeqBUS.GetNextDocSequenceNumber(DocSequence.Customer.ToString());
            txtCustomerAddress.Text = string.Empty;
            txtCustomerCMND.Text = string.Empty;
            txtCustomerEmail.Text = string.Empty;
            txtCustomerNote.Text = string.Empty;
            txtCustomerPhone.Text = string.Empty;
            dbiDebt.Value = 0;
            dtpCustomerBirthday.Value = DateTime.MinValue;
            dtpCustomerBeginDay.Value = DateTime.Now;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
            {
                return;
            }

            CustomerDto.CustomerName = txtCustomerName.Text;
            CustomerDto.CustomerBirthday = dtpCustomerBirthday.Value;
            CustomerDto.CustomerAddress = txtCustomerAddress.Text;
            CustomerDto.CustomerPhone = txtCustomerPhone.Text;
            CustomerDto.CustomerEmail = txtCustomerEmail.Text;
            CustomerDto.CustomerSex = rdbMale.Checked;
            CustomerDto.CustomerCMND = txtCustomerCMND.Text;
            CustomerDto.Note = txtCustomerNote.Text;
            CustomerDto.Status = true;


            if (customerBus.UpdateCustomer(CustomerDto))
            {
                Global.SetMessage(lblMessage, "Cập nhật thành công!", true);
                //OnCategoryChanged(Category);
                Customer.FrmCustomerList frmList = (Customer.FrmCustomerList)Application.OpenForms["FrmCustomerList"];
                if (frmList != null)
                {
                    frmList.CustomerChanged(CustomerDto);
                }
            }
            else
            {
                Global.SetMessage(lblMessage, "Cập nhật không thành công!", false);
            }
        }

        private bool CheckDataInput()
        {
            if (string.IsNullOrEmpty(txtCustomerCode.Text) || string.IsNullOrEmpty(txtCustomerName.Text)
                || string.IsNullOrEmpty(txtCustomerAddress.Text) || dtpCustomerBirthday.IsEmpty)
            {
                Global.SetMessage(lblMessage, "Các trường có (*) phải được điền đầy đủ!", false);
                txtCustomerName.Focus();
                return false;
            }
            if(string.IsNullOrEmpty(txtCustomerEmail.Text) || !Global.TestEmailRegex(txtCustomerEmail.Text))
            {
                Global.SetMessage(lblMessage, "Email chưa hợp lệ!", false);
                txtCustomerEmail.Focus();
                return false;
            }
            if (!Global.NumberOnlyRegex(txtCustomerPhone.Text))
            {
                Global.SetMessage(lblMessage, "Số điện thoại chưa hợp lệ!", false);
                txtCustomerPhone.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCustomerCMND.Text) || !Global.NumberOnlyRegex(txtCustomerCMND.Text))
            {
                Global.SetMessage(lblMessage, "Số CMND chưa hợp lệ!", false);
                txtCustomerCMND.Focus();
                return false;
            }
            return true;
        }
    }
}
